package zjc.bookkeeping.minepage.dAccountbookmanagement;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.DialogFragment;

import zjc.bookkeeping.R;
import zjc.bookkeeping.db.AccountBean;
import zjc.bookkeeping.db.ConsumptionClassBean;
import zjc.bookkeeping.db.DBManager;
import zjc.bookkeeping.db.UserBean;
import zjc.bookkeeping.model.Account;
import zjc.bookkeeping.model.User;
import zjc.bookkeeping.server.Network;
import zjc.bookkeeping.server.StaticData;
import zjc.bookkeeping.service.AccountService;
import zjc.bookkeeping.service.imp.AccountServiceImp;
import zjc.bookkeeping.tools.ToolsClass;

public class AddbookDialog extends Dialog implements View.OnClickListener {
    AccountBean accountBean = new AccountBean();
    RadioGroup radioGroup;
    RadioButton oneBtn,manyBtn;
    EditText nameEt;
    Button sureBtn,cancelBtn;
    AccountService accountService = new AccountServiceImp();
    UserBean userBean = new UserBean();
    boolean online = false;
    public AddbookDialog(@NonNull Context context) {
        super(context);
    }

    public interface AddAccountSure{
        void onSureListener();
    }
    AddAccountSure addAccountSure;

    public void setAddAccountSure(AddAccountSure addAccountSure) {
        this.addAccountSure = addAccountSure;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_add_account);
        initView();
    }

    private void initView() {
        radioGroup = findViewById(R.id.dialog_account_rg);
        oneBtn = findViewById(R.id.dialog_account_one);
        manyBtn = findViewById(R.id.dialog_account_many);
        nameEt = findViewById(R.id.dialog_account_name);
        sureBtn = findViewById(R.id.dialog_account_sure_btn);
        cancelBtn = findViewById(R.id.dialog_account_cancel_btn);

        sureBtn.setOnClickListener(this);
        cancelBtn.setOnClickListener(this);

        radioGroup.check(oneBtn.getId());
        accountBean.setAccount_kind(0);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                RadioButton radioButton = findViewById(i);
                if (radioButton.getText().toString().equals("单人账本")){
                    accountBean.setAccount_kind(0);
                }else {
                    accountBean.setAccount_kind(1);
                }
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.dialog_account_cancel_btn:
                cancel();
                break;
            case R.id.dialog_account_sure_btn:
                userBean = DBManager.findUser();
                Thread thread = new Thread(new Runnable() {
                    @Override
                    public void run() {
                        online = Network.isOnline();
                    }
                });
                thread.start();
                try {
                    thread.join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (nameEt.getText().toString().isEmpty()){
                    ToolsClass.showToast(getContext(),"请输入账本名.", Toast.LENGTH_SHORT);
                    return;
                }else {
                    if (!online){
                        ToolsClass.showToast(getContext(),"网络不可用，请检查网络！", Toast.LENGTH_SHORT);
                        return;
                    }
                    if(userBean == null){
                        ToolsClass.showToast(getContext(),"用户未登录，请登录后使用！", Toast.LENGTH_SHORT);
                        return;
                    }
                    accountBean.setAccount_name(nameEt.getText().toString());
                    Account account = new Account();
                    String uid = null;
                    account.setAccountId("");
                    account.setAccountName(accountBean.getAccount_name());
                    account.setAccountRemarks("");
                    account.setAccountTime(ToolsClass.getYMDTime(0));
                    account.setAccountType(accountBean.getAccount_kind());
                    account.setAccountUid("");
                    account.setGrade(3);
                    account.setUserId(userBean.getUser_id());
                    account.setUserRemarks(userBean.getUser_name());
                    uid = accountService.addAccount(account);
                    Log.i("uid",uid+"");
                    DBManager.insertAccount(accountBean.getAccount_name(),accountBean.getAccount_kind(),uid);
                    addAccountSure.onSureListener();
                }
                cancel();
                break;
        }
    }
    public void setDialogSize(){
        Window window =getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
        Display display = window.getWindowManager().getDefaultDisplay();
        layoutParams.width = (int) (display.getWidth());
        layoutParams.gravity = Gravity.CENTER;
        window.setAttributes(layoutParams);
    }
}
